package com.u3k.app.mid.utils.net;

import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.LogUtils;
import com.u3k.mid.ae;

import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Locale;
import javax.net.ssl.HttpsURLConnection;

public class HttpDownloader extends HttpTask {
    static String TAG = HttpDownloader.class.getSimpleName();
    DownloadCallback callback;
    HttpClient client;
    Request request;
    String savePath;

    public HttpDownloader(HttpClient httpClient, Request request, String str, DownloadCallback downloadCallback) {
        this.client = httpClient;
        this.request = request;
        this.savePath = str;
        this.callback = downloadCallback;
    }

    void downloadFromBreakPoint() {
        long length;
        long j;
        HttpURLConnection httpURLConnection;
        RandomAccessFile randomAccessFile;
        HttpURLConnection httpURLConnection2;
        RandomAccessFile randomAccessFile2;
        InputStream inputStream;
        Exception exception;
        boolean z;
        Throwable th;
        RandomAccessFile randomAccessFile3;
        Throwable th2;
        LogUtils.LogError(TAG, "downloadFromBreakPoint");
        InputStream inputStream2 = null;
        File file = new File(this.savePath);
        if (file.exists()) {
            length = file.length();
            LogUtils.LogError(TAG, "downloadFromBreakPoint  file exists, file.length=" + length);
            j = length;
        } else {
            File parentFile = file.getParentFile();
            if (parentFile == null) {
                LogUtils.LogError(TAG, "downloadFromBreakPoint  getParentFile failed");
                if (this.callback != null) {
                    this.callback.onFailure(-1, null);
                    return;
                }
                return;
            }
            if (!parentFile.exists()) {
                LogUtils.LogError(TAG, "downloadFromBreakPoint  mkdirs dir=" + parentFile.getAbsolutePath());
                parentFile.mkdirs();
            }
            length = 0;
            j = 0;
        }
        try {
            LogUtils.LogError(TAG, "downloadFromBreakPoint  begin=");
            URL url = new URL(this.request.url);
            if (url.getProtocol().toLowerCase(Locale.getDefault()).equals("https")) {
                HttpCall.trustAllHosts();
                HttpURLConnection httpURLConnection3 = (HttpsURLConnection) url.openConnection();
                httpURLConnection3.setHostnameVerifier(HttpCall.DO_NOT_VERIFY);
                httpURLConnection = httpURLConnection3;
            } else {
                httpURLConnection = (HttpURLConnection) url.openConnection();
            }
            try {
                httpURLConnection.setConnectTimeout(this.client.getConnectTimeout());
                httpURLConnection.setReadTimeout(this.client.getReadTimeout());
                httpURLConnection.setUseCaches(true);
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoInput(true);
                httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                httpURLConnection.addRequestProperty("Connection", "Keep-Alive");
                if (length > 0) {
                    httpURLConnection.setRequestProperty("Range", "bytes=" + length + "-");
                }
                int responseCode = httpURLConnection.getResponseCode();
                LogUtils.LogError(TAG, "downloadFromBreakPoint  responseCode=" + responseCode);
                if (200 == responseCode || 206 == responseCode) {
                    responseCode = httpURLConnection.getContentLength();
                    LogUtils.LogError(TAG, "downloadFromBreakPoint  contentLength=" + responseCode);
                    if (length == ((long) responseCode)) {
                        httpURLConnection.disconnect();
                        if (this.callback != null) {
                            this.callback.onDownloaded();
                        }
                        if (httpURLConnection != null) {
                            httpURLConnection.disconnect();
                            return;
                        }
                        return;
                    }
                    if (length > ((long) responseCode)) {
                        LogUtils.LogError(TAG, "downloadFromBreakPoint  downloadLength > contentLength  delete");
                        file.delete();
                        length = 0;
                        j = 0;
                    } else {
                        long j2 = length;
                        length = j;
                        j = j2;
                    }
                    randomAccessFile = new RandomAccessFile(file, "rwd");
                    try {
                        randomAccessFile.seek(j);
                        inputStream2 = httpURLConnection.getInputStream();
                    } catch (Exception e) {
                        httpURLConnection2 = httpURLConnection;
                        randomAccessFile2 = randomAccessFile;
                        inputStream = null;
                        exception = e;
                        try {
                            LogUtils.LogError(TAG, "downloadFromBreakPoint  Exception=", exception);
                            if (this.callback != null) {
                                this.callback.onFailure(0, exception.getMessage());
                            }
                            z = true;
                            StatisticsManager.getInstance().logException(ae.a(), exception);
                            if (inputStream != null) {
                                try {
                                    inputStream.close();
                                } catch (Exception exception2) {
                                    exception2.printStackTrace();
                                    if (httpURLConnection2 != null) {
                                        httpURLConnection2.disconnect();
                                    }
                                    LogUtils.LogError(TAG, "downloadFromBreakPoint  failed=" + z);
                                    if (z) {
                                    }
                                    return;
                                }
                            }
                            if (randomAccessFile2 != null) {
                                randomAccessFile2.close();
                            }
                            if (httpURLConnection2 != null) {
                                httpURLConnection2.disconnect();
                            }
                            LogUtils.LogError(TAG, "downloadFromBreakPoint  failed=" + z);
                            if (z) {
                            }
                            return;
                        } catch (Throwable th3) {
                            inputStream2 = inputStream;
                            th = th3;
                            randomAccessFile3 = randomAccessFile2;
                            httpURLConnection = httpURLConnection2;
                            if (inputStream2 != null) {
                                try {
                                    inputStream2.close();
                                } catch (Exception e2) {
                                    e2.printStackTrace();
                                    if (httpURLConnection != null) {
                                        httpURLConnection.disconnect();
                                    }
                                    throw th;
                                }
                            }
                            if (randomAccessFile3 != null) {
                                randomAccessFile3.close();
                            }
                            if (httpURLConnection != null) {
                                httpURLConnection.disconnect();
                            }
                            throw th;
                        }
                    } catch (Throwable th32) {
                        th2 = th32;
                        randomAccessFile3 = randomAccessFile;
                        th = th2;
                        if (inputStream2 != null) {
                            inputStream2.close();
                        }
                        if (randomAccessFile3 != null) {
                            randomAccessFile3.close();
                        }
                        if (httpURLConnection != null) {
                            httpURLConnection.disconnect();
                        }
                        throw th;
                    }
                    try {
                        byte[] bArr = new byte[4096];
                        long j3 = length;
                        while (true) {
                            int read = inputStream2.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            randomAccessFile.write(bArr, 0, read);
                            j3 += (long) read;
                            read = ((int) ((((float) j3) * 100.0f) / ((float) responseCode))) / 2;
                        }
                        z = false;
                    } catch (Exception e22) {
                        httpURLConnection2 = httpURLConnection;
                        randomAccessFile2 = randomAccessFile;
                        inputStream = inputStream2;
                        exception2 = e22;
                        LogUtils.LogError(TAG, "downloadFromBreakPoint  Exception=", exception2);
                        if (this.callback != null) {
                            this.callback.onFailure(0, exception2.getMessage());
                        }
                        z = true;
                        StatisticsManager.getInstance().logException(ae.a(), exception2);
                        if (inputStream != null) {
                            inputStream.close();
                        }
                        if (randomAccessFile2 != null) {
                            randomAccessFile2.close();
                        }
                        if (httpURLConnection2 != null) {
                            httpURLConnection2.disconnect();
                        }
                        LogUtils.LogError(TAG, "downloadFromBreakPoint  failed=" + z);
                        if (z) {
                            return;
                        }
                    } catch (Throwable th322) {
                        th2 = th322;
                        randomAccessFile3 = randomAccessFile;
                        th = th2;
                        if (inputStream2 != null) {
                            inputStream2.close();
                        }
                        if (randomAccessFile3 != null) {
                            randomAccessFile3.close();
                        }
                        if (httpURLConnection != null) {
                            httpURLConnection.disconnect();
                        }
                        throw th;
                    }
                }
                LogUtils.LogError(TAG, "downloadFromBreakPoint  onFailure=");
                if (this.callback != null) {
                    this.callback.onFailure(responseCode, null);
                }
                z = true;
                inputStream2 = null;
                randomAccessFile = null;
                if (inputStream2 != null) {
                    try {
                        inputStream2.close();
                    } catch (Exception exception22) {
                        exception22.printStackTrace();
                    }
                }
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
            } catch (Exception e222) {
                httpURLConnection2 = httpURLConnection;
                randomAccessFile2 = null;
                inputStream = null;
                exception22 = e222;
                LogUtils.LogError(TAG, "downloadFromBreakPoint  Exception=", exception22);
                if (this.callback != null) {
                    this.callback.onFailure(0, exception22.getMessage());
                }
                z = true;
                StatisticsManager.getInstance().logException(ae.a(), exception22);
                if (inputStream != null) {
                    inputStream.close();
                }
                if (randomAccessFile2 != null) {
                    randomAccessFile2.close();
                }
                if (httpURLConnection2 != null) {
                    httpURLConnection2.disconnect();
                }
                LogUtils.LogError(TAG, "downloadFromBreakPoint  failed=" + z);
                if (z) {
                }
                return;
            } catch (Throwable th3222) {
                th = th3222;
                randomAccessFile3 = null;
                if (inputStream2 != null) {
                    inputStream2.close();
                }
                if (randomAccessFile3 != null) {
                    randomAccessFile3.close();
                }
                if (httpURLConnection != null) {
                    httpURLConnection.disconnect();
                }
                throw th;
            }
        } catch (Exception e2222) {
            httpURLConnection2 = null;
            randomAccessFile2 = null;
            inputStream = null;
            exception22 = e2222;
            LogUtils.LogError(TAG, "downloadFromBreakPoint  Exception=", exception22);
            if (this.callback != null) {
                this.callback.onFailure(0, exception22.getMessage());
            }
            z = true;
            StatisticsManager.getInstance().logException(ae.a(), exception22);
            if (inputStream != null) {
                inputStream.close();
            }
            if (randomAccessFile2 != null) {
                randomAccessFile2.close();
            }
            if (httpURLConnection2 != null) {
                httpURLConnection2.disconnect();
            }
            LogUtils.LogError(TAG, "downloadFromBreakPoint  failed=" + z);
            if (z) {
                return;
            }
        } catch (Throwable th32222) {
            httpURLConnection = null;
            th = th32222;
            randomAccessFile3 = null;
            if (inputStream2 != null) {
                inputStream2.close();
            }
            if (randomAccessFile3 != null) {
                randomAccessFile3.close();
            }
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
            throw th;
        }
        LogUtils.LogError(TAG, "downloadFromBreakPoint  failed=" + z);
        if (z && this.callback != null) {
            this.callback.onDownloaded();
        }
    }

    void execute() {
        downloadFromBreakPoint();
    }

    public String host() {
        return this.request.host();
    }

    public final void run() {
        try {
            if (this.callback != null) {
                this.callback.onDownloadBegin();
            }
            execute();
        } catch (Exception e) {
            if (this.callback != null) {
                this.callback.onFailure(-1, e.getMessage());
            }
            StatisticsManager.getInstance().logException(ae.a(), e);
        } finally {
            this.client.callFinished(this);
        }
    }
}
